

package controller;




import bo.BUS_SanPham;
import com.opensymphony.xwork2.ActionSupport;
import dto.DTO_GioHang;
import dto.DTO_SanPham;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.SessionAware;



public class GioHangAction extends ActionSupport implements ServletRequestAware , SessionAware {

    private Map session;
    private HttpServletRequest request;
  
    private double tongTien =0;
    private int tongSP =0;

    public int getTongSP() {
        return tongSP;
    }

    public void setTongSP(int tongSP) {
        this.tongSP = tongSP;
    }

    public double getTongTien() {
        return tongTien;
    }

    public void setTongTien(double tongTien) {
        this.tongTien = tongTien;
    }
  
    public GioHangAction() {

    }



    public String execute() throws Exception {
        //neu action khac null
       if(request.getParameter("action")!= null && session.get("KhachHang") != null)
       {
           //lay thong so action
           String action = request.getParameter("action");
           //neu la action = add
           if(action.equals("add"))
           {
               DTO_GioHang giohang = (DTO_GioHang)session.get("GioHang");
               if(giohang == null)
               {
                   giohang = new DTO_GioHang();
               }
               int id = Integer.parseInt(request.getParameter("id"));
               BUS_SanPham busSP = new BUS_SanPham();
               DTO_SanPham sanpham = busSP.LayThongTinSanPham(id);
               giohang.themSanPham(sanpham);
               //ting tong tien va tong san pham
               for(DTO_SanPham sp : giohang)
               {
                    tongSP += sp.getSoLuong();
                    tongTien += (sp.getSoLuong()*sp.getGiaBan());
               }
               session.put("TongSP", tongSP);
               session.put("TongTien", tongTien);

               session.put("GioHang", giohang);
               return "success";
           }else{
               if(action.equals("remove"))
               {
                   String id = request.getParameter("id");
                   if(session.get("GioHang")!= null)
                   {
                       DTO_GioHang giohang = (DTO_GioHang)session.get("GioHang");
                       for(DTO_SanPham sp : giohang)
                       {
                           if(sp.getMaSP() == Integer.parseInt(id))
                           {
                               giohang.remove(sp);
                               break;
                           }
                       }
                        //ting tong tien va tong san pham
                       for(DTO_SanPham sp : giohang)
                       {
                            tongSP += sp.getSoLuong();
                            tongTien += (sp.getSoLuong()*sp.getGiaBan());
                       }
                       session.put("TongSP", tongSP);
                       session.put("TongTien", tongTien);
                       session.put("GioHang", giohang);
                   }

                   return "success";
               }else{
                   if(action.equals("update"))
                   {
                      int sl;
                      try
                      {
                         sl = Integer.parseInt(request.getParameter("soluong"));
                       }
                      catch(Exception e)
                      {
                          return "error";
                      }
                      int id = Integer.parseInt(request.getParameter("id"));
                      if(session.get("GioHang")!= null)
                      {
                           DTO_GioHang giohang = (DTO_GioHang)session.get("GioHang");
                           for(DTO_SanPham sp : giohang)
                           {
                               if(sp.getMaSP() == id)
                               {
                                   sp.setSoLuong(sl);
                                   break;
                               }
                          }

                        //ting tong tien va tong san pham
                       for(DTO_SanPham sp : giohang)
                       {
                            tongSP += sp.getSoLuong();
                            tongTien += (sp.getSoLuong()*sp.getGiaBan());
                       }
                       session.put("TongSP", tongSP);
                       session.put("TongTien", tongTien);
                       session.put("GioHang", giohang);
                      }

                      return "success";

                   }
               }
           }

       }
       return "error";

    }

    @Override
    public void setServletRequest(HttpServletRequest hsr) {
        request = hsr;
    }

    @Override
    public void setSession(Map map) {
        session = map;
    }



}